﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Specialized;
using eAssessment.questions;
using eAssessment.lists;
using System.Collections.Generic;
using System.Drawing;
using eAssessment.Classes;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Web;
using eAssessment.Classes.Misc;

namespace eAssessment.ShowMChoiceQuestion
{
    public partial class ShowMChoiceQuestionUserControl : UserControl
    {
        private MultipleChoiceQuestionTemplate _question;

        protected void Page_Load(object sender, EventArgs e)
        {
            string id = Request.QueryString["qid"];
            if (id != null)
            {
                ListAccessManager listmanager = new ListAccessManager();
                _question = listmanager.GetMultipleChoiceQuestionTemplate(id);
                if (_question != null)
                {
                    try
                    {
                        questionTextbox.Text = _question.QuestionText;
                        Dictionary<string, bool> answers = _question.Answers;
                        foreach (string key in answers.Keys)
                        {
                            bool check;
                            answers.TryGetValue(key, out check);

                            TableRow row = new TableRow();

                            TableCell textCell = new TableCell();
                            textCell.Text = key;

                            TableCell checkCell = new TableCell();
                            CheckBox box = new CheckBox();
                            checkCell.Controls.Add(box);

                            row.Cells.Add(textCell);
                            row.Cells.Add(checkCell);
                            answerTable.Rows.Add(row);
                        }
                    }
                    catch (Exception ex)
                    {
                        answerTable_errorMessage.Text = Language.getString("error_createTable");
                        Console.WriteLine(ex);
                    }
                }
                else
                {
                    questionTextbox.Text = Language.getString("error_showId");
                }
            }
            else
            {
                questionTextbox.Text = Language.getString("text_default_MChoiceQuestion");
            }
        }

        protected void onSendClick(object sender, EventArgs e)
        {
            for (int i = 1; i <= _question.Answers.Count; i++)
            {
                bool userInput = false;
                foreach (CheckBox cb in answerTable.Rows[i].Cells[1].Controls)
                {
                    userInput = cb.Checked;
                }
                bool correctAnswer;
                string key = answerTable.Rows[i].Cells[0].Text;
                _question.Answers.TryGetValue(key, out correctAnswer);
                if (correctAnswer == userInput)
                {
                    answerTable.Rows[i].Cells[0].ForeColor = Color.Green;
                }
                else
                {
                    answerTable.Rows[i].Cells[0].ForeColor = Color.Red;
                }
            }
        }

    }
}
